package com.study.animationstudy.ui

import android.annotation.SuppressLint
import android.graphics.drawable.Drawable
import android.graphics.drawable.TransitionDrawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.study.animationstudy.R
import com.study.animationstudy.base.BaseActivity
import com.study.animationstudy.databinding.ActivityFadeAnimBinding

class FadeAnimActivity : BaseActivity<ActivityFadeAnimBinding>() {

    override fun ActivityFadeAnimBinding.initBinding() {
        mBinding.ivFadeAnim.setOnClickListener {
            showFadeAnimation()
        }
        showFadeAnimation()
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    fun showFadeAnimation(){
        // 淡入淡出动画需要先定义一个图形资源数组，用于变换图片
        val drawableArr = arrayOf(getDrawable(R.drawable.fade_begin),getDrawable(R.drawable.fade_end))
        // 创建一个用于淡入淡出动画的过渡图形
        val transDrawable = TransitionDrawable(drawableArr)
        //设置过渡图形
        mBinding.ivFadeAnim.setImageDrawable(transDrawable)
        // 是否启用交叉淡入。启用后淡入效果更柔和
        transDrawable.isCrossFadeEnabled = true
        // 开始时长3秒的过渡转换
        transDrawable.startTransition(3000)
    }
}